import {createClient, RediSearchSchema} from 'redis'

interface CreateOptions {
    ON?: 'HASH' | 'JSON';
    PREFIX?: string | Array<string>;
    FILTER?: string;
}
// const REDIS_PORT = 6379
const client = createClient()
client.connect()
class Cache {
    client = client
    async setShortTime(k: string, v: any) {
        await client.setEx(k, 10, v)
    }
    async setLongTime(k: string, v: any) {
        await client.json.set(k, '$', v,)
    }

    async get(k: string) {
        return await client.json.get(k)
    }
}

export const cache = client
export const createIndex = async (...args: Parameters<typeof cache.ft.create>) => {
    let [index, schema, options] = args
    try {
        await cache.ft.create(index as any, schema as any, options)
    } catch (e) {
        if (e.message === 'Index already exists') {
            console.log(`Index ${index} exists already, skipped creation.`);
        } else {
            // Something went wrong, perhaps RediSearch isn't installed...
            console.error(e);
            process.exit(1);
        }
    }
}
export const r_ = (str: string) => {return str.replace(/([~!@\$%\^&\*\.\(\)\-\+='\"\[\]\{\}\\:|;])/g, '\\$1')}

